var app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    screenWidth: 350,
    leftitems: [],
    rightitems: [],
    in_openid:'', // 页面获取id
    avatarUrl:'',
    nickName:'',
    follow:[],
    flow: '关注',
  },
  async onLoad(option){
    let in_openid = option.in_openid
     this.get(in_openid)
     this.setData({
      in_openid
     })
  },
  async flown() {
    let { flow, follow,openid, in_openid} = this.data
    if (flow == '已关注') {
      flow = '关注';
      follow.splice(follow.indexOf(openid), 1)
      console.log(follow.indexOf(openid))
      console.log(follow)
    } else {
      flow = '已关注'
      if(follow.indexOf(openid)==-1){
      follow.push(openid)
      }
      console.log(follow)
    }
    this.setData({
      flow,
      follow
    })
    let res = await wx.cloud.callFunction({
      name: 'upFollow',
      data: {
        openid,
        in_openid,
        follow
      }
    })
  },
  async onPullDownRefresh(){
    
    await this.get(in_openid)
    wx.stopPullDownRefresh()

  },
  async get(in_openid){
    wx.showLoading({
      title: '努力加载中...',
      mask:true
    })
    let openid = app.globalData.openid
    console.log(openid)
    let res = await wx.cloud.callFunction({
      name:'getPersonPage',
      data:{
        in_openid,
        openid
      }
    })
    console.log(res)
    var screenWidth = await wx.getSystemInfoSync().screenWidth; //计算宽度
    this.setData({
      screenWidth: screenWidth / 2-7,
      leftitems:res.result.leftitems,
      rightitems:res.result.rightitems,
      avatarUrl:res.result.user.avatarUrl,
      nickName:res.result.user.nickName,
      follow:res.result.user.follow,
      flow:res.result.flow,
      openid,
      in_openid
    });
     wx.hideLoading()
  }
})